/*
 * The following functions CreateCookie, ReadCookie and CheckForCookies
 * are all called from default and search page, to check for the existance
 * of whether cookies are available.
 */

/*
 * WI 51911 - Add Distance to Provider Results
 *  Cookie Values for Shopping basket and Compare
 *  are now of the form ID|POSTCODE|DISTANCE
 */
 
/* The time paramater is in milliseconds */
function CreateCookie(name, value, time)
{
    if (time)
    {
	    var date = new Date();
	    date.setTime(date.getTime()+ time);
	    var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

/* This function attempts to read the cookie */
function ReadCookie(sCookieName)
{
    var sNameEQ = sCookieName + "=";
    var peCookies = document.cookie.split(';');
    for(var i = 0; i < peCookies.length; i++)
    {
	    var peCookie = peCookies[i];
	    while (peCookie.charAt(0) == ' ')
	        peCookie = peCookie.substring(1, peCookie.length);
	    if (peCookie == sCookieName || peCookie.indexOf(sNameEQ) == 0)
	        return true;
	}
	return false;
}

function CheckForCookies()
{
    // To check for a cookie - we need to create a temporary one
    // and then attempt to read it
    CreateCookie("PETestCookie", "", (1*60*1000)); // That will expire in 1 minute
    
    // Now attempt to read it
    if(ReadCookie("PETestCookie") == false)
        return false;
    
    // Now delete it
    // To clear the cookie - just recreate it with an expiry of yesterday
    // The browser will then see that it has expired and then delete it.
    CreateCookie("PETestCookie", "", (-1*24*60*60*1000)); // Set expiry to a day old
    
    return true;
}

/*
 * The following section of functions are called from within the search page.
 *
 * WI 51911 - Add distance to Provider Results
 *            Id is form ID|POSTCODE|DISTANCE
 */
function DoesIdExist(sExistingIds, sCheckboxId)
{
    /* Extract the ID from the checkbox */
    var sCheckboxIdSplit = sCheckboxId.split('|');
    var sId = sCheckboxIdSplit[0];

    var sOldSplit = sExistingIds.split(',');
    for(var i = 0; i < sOldSplit.length; i++)
    {
        /* We now have ID|POSTCOE|DISTANCE */
        var sExistingSplit = sOldSplit[i].split('|');
        var sOldId = sExistingSplit[0];
        if (sOldId == sId)
            return true;
    }
    return false;
}

/* Function that returns unique provider ids */
function GetUniqueIds(sExistingIds, sNewIds)
{
    if (sExistingIds == "")
        return sNewIds;
        
    var sIDS = sExistingIds;
    
    var sNewSplit = sNewIds.split(',');
    for(var i = 0; i < sNewSplit.length; i++)
    {
        var sCheckboxId = sNewSplit[i];
        // If the id doesn't already exist in the list then add it
        if (!DoesIdExist(sExistingIds, sCheckboxId))
            sIDS += ("," + sCheckboxId);
    }
    
    return sIDS;
}

/*
 *
 * Cookies are saved in the format of
 * name=value;expiry date;path
 * for example "PESearchCookie=1,3,10;expires=Thu, 2 Aug 2001 20:47:11 UTC; path=/"
 */
function GetExistingCookieValue(sCookieName)
{
    var sNameEQ = sCookieName + "=";
    var peCookies = document.cookie.split(';');
    for(var i = 0; i < peCookies.length; i++)
    {
	    var peCookie = peCookies[i];
	    while (peCookie.charAt(0) == ' ')
	        peCookie = peCookie.substring(1, peCookie.length);
	    if (peCookie.indexOf(sNameEQ) == 0)
	        return peCookie.substring(sNameEQ.length, peCookie.length);
    }
    
    return "";
}

/* WI 42286 - Add Comparison Link - 
   Renamed from SaveToShopping Basket to SaveCSVToCookie
   Also modified GetExistingCookieValue to use param coming in rather 
   than hard coded "PESearchCookie" */
function SaveCSVToCookie(sCookieName, sIDS)
{
    if(sIDS == "")
        return false; // Nothing to do

    var sExistingIds = GetExistingCookieValue(sCookieName);
    var sNewIds = GetUniqueIds(sExistingIds, sIDS);
    // Create the cookie with the list of ids
    var expires = "";   // The cookie will expire when the browser closes
    document.cookie = sCookieName + "=" + sNewIds + expires +"; path=/";
    
    return true;
}

function SelectLanguage(iLangId)
{
    CreateCookie("PELanguageCookie", iLangId);
    return;
}

/* WI 42286 - Add Comparison Link 
   Recreate the cookie with the new list of provider ids's.
   As validation is done prior to this function, the list of ids
   can never be empty - so we don't need to check. */
function SaveCompareCookie(sIDS)
{
    // First clear the old cookie
    CreateCookie("PECompareCookie", "", (-1 * 24 * 60 * 60 * 1000)); // Set expiry to a day old
    // Now save the new cookie
    SaveCSVToCookie("PECompareCookie", sIDS);
    return;
}